import request from '@/utils/request'
import { getCustomerInfo } from '@/api/crm/customer.js'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { previewDataInterface } from '@/api/systemData/dataInterface'
import { getSelector } from '@/api/crm/school'
import payment from '../../payment'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      setting: {},
      visible: false,
      isDetail: false,
      type: false,
      paymentId: undefined,
      dataForm: {
        id: '',
        area: undefined,
        customerId: undefined,
        customerName: undefined,
        brand: undefined,
        managerId: undefined,
        school: undefined,
        contractPolicy: undefined,
        orderPayment: {
          bond: undefined,
          isBond: false,
          // balance: '',
          // isBalance: false,
          paymentDate: undefined,
          amount: undefined,
          // paymentType: undefined,
          paymentMethod: undefined,
          screenshot: [],
          isCompletepay: false
        }
      },
      rules: {
        customerId: [
          {
            required: true,
            message: '请选择客户',
            trigger: 'change'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入收款品牌',
            trigger: 'change'
          }
        ],
        managerId: [
          {
            required: true,
            message: '请选择招商经理',
            trigger: 'change'
          }
        ],
        contractPolicy: [
          {
            required: true,
            message: '请输入加盟政策',
            trigger: 'blur'
          }
        ],
        school: [
          {
            required: true,
            message: '请输入意向小学/中学',
            trigger: 'blur'
          }
        ],
        'orderPayment.payMethod': [
          {
            required: true,
            message: '请选择付款方式',
            trigger: 'change'
          }
        ],
        // 'orderPayment.paymentType': [
        //   {
        //     required: true,
        //     message: '请选择收款类型',
        //     trigger: 'blur'
        //   }
        // ],
        'orderPayment.amount': [
          {
            required: true,
            message: '请输入已收款金额',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        'orderPayment.paymentDate': [
          {
            required: true,
            message: '请选择收款日期',
            trigger: 'change'
          }
        ],
        'orderPayment.paymentMethod': [
          {
            required: true,
            message: '请选择支付方式',
            trigger: 'change'
          }
        ],
        'orderPayment.bond': [
          {
            validator: (rule, value, callback) => {
              if (this.dataForm.orderPayment.isBond && !value) {
                return callback(new Error('请输入保证金金额'))
              } else {
                return callback()
              }
            },
            trigger: 'blur'
          }
        ]
        // 'orderPayment.balance': [
        //   {
        //     validator: (rule, value, callback) => {
        //       if (this.dataForm.orderPayment.isBalance && !value) {
        //         return callback(new Error('请输入余款金额'))
        //       } else {
        //         return callback()
        //       }
        //     },
        //     trigger: 'blur'
        //   }
        // ]
      },
      brandOptions: [],
      methodOptions: [{ fullName: '刷卡', id: '1' }, { fullName: '转账', id: '2' }, { fullName: '扫码', id: '3' }, { fullName: '现金', id: '4' }],
      managerIdOptions: []
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.managerIdOptions = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    goBack() {
      this.$emit('refresh')
    },
    init(data) {
      this.setting = data
      this.dataForm.id = data.id || ''
      this.type = data.typeValue
      this.$refs['elForm'].resetFields()
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.id) {
          request({
            url: '/api/crm/orders/payment/' + data.id,
            method: 'get'
          }).then(res => {
            getCustomerInfo(res.data.customerId).then(customer => {
              this.dataForm = res.data
              this.dataForm.customerName = customer.data.name
              this.dataForm = Object.assign(this.dataForm, {
                orderPayment: res.data.orderPayment
              })
            })
          })
        } else {
          this.dataForm.flowId = data.flowId
          if (this.type == 'addOrder') {
            this.dataForm.customerId = data.customer.id
            this.dataForm.customerName = data.customer.name
            this.dataForm.managerId = data.customer.managerId
            this.dataForm.area = data.customer.area
          } else {
            this.dataForm = Object.assign(this.dataForm, data.order)
          }
        }
      })
    },
    dataFormSubmit(eventType) {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (eventType === 'audit' || eventType === 'reject') {
            this.$emit('eventReciver', this.dataForm, eventType)
            return
          }
          this.dataForm.orderPayment.flowState = eventType === 'submit' ? 0 : 1
          this.eventType = eventType
          if (this.eventType === 'submit') {
            this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
              type: 'warning'
            })
              .then(() => {
                this.request()
              })
              .catch(() => {})
          } else {
            this.request()
          }
        }
      })
    },
    request() {
      if (this.type == 'addOrder') {
        request({
          url: `/api/crm/orders`,
          method: 'post',
          data: this.dataForm
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.$emit('close', true)
            }
          })
        })
      } else if (this.type == 'addPayment') {
        request({
          url: `/api/crm/orders/${this.dataForm.customerId}/${this.dataForm.id}/payment`,
          method: 'post',
          data: this.dataForm
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.$emit('close', true)
            }
          })
        })
      } else {
        request({
          url: `/api/crm/orders/${this.dataForm.id}/payment`,
          method: 'put',
          data: this.dataForm.orderPayment
        }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.$emit('close', true)
            }
          })
        })
      }
    },
    judgeShow(id) {
      if (!this.setting.formOperates || !this.setting.formOperates.length) return true
      let arr = this.setting.formOperates.filter(o => o.id === id) || []
      if (!arr.length) return true
      let item = arr[0]
      return item.read
    },
    judgeWrite(id) {
      if (!this.setting.formOperates || !this.setting.formOperates.length) return false
      let arr = this.setting.formOperates.filter(o => o.id === id) || []
      if (!arr.length) return true
      let item = arr[0]
      return !item.write
    }
  }
}
